package commonClass;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import android.util.Log;
import Modle.disease.ClinicalSymptomsKey;
import Modle.disease.Symptoms;

public class CopyOfADAQueryDiseaseIDAndDiseaseProbabilityByConditionsAction002 implements IAnimalDoctorAction {

	
	
	@Override	
	public String object2json(ArrayList<Object> list) {
		
		String jsonString="";
		jsonString = "animalID=" + (String) list.get(0);
		
		ArrayList<Object> jsonObject = new ArrayList<Object>();
		jsonObject = (ArrayList<Object>) list.get(1);
		ArrayList<Object> jsonObject2 = new ArrayList<Object>();
		jsonObject2 =  (ArrayList<Object>) list.get(2);
		List<String> jsonstringofthis = new ArrayList<String>(); 


		
		for(int i = 0 ; i < jsonObject.size() ; i ++){
			
			jsonstringofthis.add((jsonObject.get(i)).toString());
			Log.v("22222",(jsonObject.get(i)).toString() );
		}
		
		int kkkk = jsonObject2.size();
		System.out.println("list chang du is " + kkkk);
		for(int x = 0 ; x < jsonObject2.size() ; x++){
			
			jsonstringofthis.add((jsonObject2.get(x)).toString());
			Log.v("22222",(jsonObject2.get(x)).toString() );
		}
	
		

		
		 /*JSONObject json = new JSONObject();
		String jsons = "";
		try {
			json.put("conditions", new Gson().toJson(jsonstringofthis));
			jsons = json.toString().replace("\\","");

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(json.toString());*/
		
		
	
		JSONObject jsonobjt = new JSONObject();
	
		String str ="[";
		for(String s:jsonstringofthis) {
			str = str + "\"" + s + "\",";
		}
		str = str.substring(0, str.lastIndexOf("\""));
		str = str + "\"" + "]";
		
		System.out.println("UUUUUUUUUUUUUUU" + str);
		Log.v("22222", str);
		/*try {
			jsonobjt.put("conditions", jsonstringofthis.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
			
		

		

		try {
				System.out.print("HLGIOIIGYGYGGI");
				
				str =URLEncoder.encode( str ,"UTF-8");
		
	

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		
			jsonString = jsonString + "&" + "conditions=" + str ;
			
/*			try {
				jsonString = URLEncoder.encode( jsonString ,"UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			System.out.println("%%%%&&&&&&&&&&&&&&" + jsonString);*/
		
//	
//		Integer j = (Integer) list.get(0);
			return jsonString ;
		
	}
		


	@Override
	public ArrayList<Object>  json2object(String json) {

		Log.v("22222", json);
		ArrayList<Object> list = new ArrayList<Object>();
		if (!jsonIsData(json))
			return null;
//		Integer i = new Integer(10);
//		list.add(i);
		list = getListFromJson(json);
		
		return list;
	}
	private ArrayList<Object> getListFromJson(String json) {

		ArrayList<Object> list = new ArrayList<Object>();

		org.json.JSONArray jsonObjs = null;
		try {
			jsonObjs = new JSONObject(json).getJSONArray("clinicalSymptomsKey");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String segJson = "";
		Log.v("000000", "getListFromJson");
		for (int i = 0; i < jsonObjs.length(); i++) {
			JSONObject jsonObj = ((JSONObject) jsonObjs.opt(i));
//			segJson = new Gson().toJson(jsonObj);
			
			
			ClinicalSymptomsKey obj = json2SymptomsCategorie1(jsonObj);
			
			list.add(obj);
		}

		return list;

		/*
		 * int id = jsonObj.getInt("id"); String name =
		 * jsonObj.getString("name"); String gender =
		 * jsonObj.getString("gender"); s += "ID号"+id + ", 姓名：" + name + ",性别："
		 * + gender+ "\n" ;
		 */

	}
	private boolean jsonIsData(String json) {
		// String json =
		// "{'response':{'data':[{'address':'南京市游乐园','province':'江苏','district':'玄武区','city':'南京'}]},'status':'ok'}";

		JSONObject jsonObj;
		String result = "";
		try {
			jsonObj = new JSONObject(json);
			result = jsonObj.getString("code");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return (result.equals("success"));
	}

	private ClinicalSymptomsKey json2SymptomsCategorie1(JSONObject jsonObj) {
		ClinicalSymptomsKey obj = new ClinicalSymptomsKey();

		// new Gson().fromJson(segJson,SymptomsCategorie.class);

		try {
//			JSONObject jsonObj = new JSONObject(segJson);

			
			obj.setDiseaseName(jsonObj.getString("diseaseName"));
			obj.setDiseaseID(jsonObj.getString("diseaseID"));
			obj.setDiseaseProbability(jsonObj.getDouble("diseaseProbability"));
/*			
			obj.setClinicalSymptomsKey(jsonObj.getString("clinicalSymptomsKey"));
			obj.setClinicalSymptomsKeyID(jsonObj.getString("clinicalSymptomsKeyID"));
			obj.setDiseaseProbability(jsonObj.getInt("diseaseProbability"));*/
			
/*		obj.setAnimalID(jsonObj.getString("animalID"));
			obj.setAnimalName((jsonObj.getString("animalName")));
			obj.setIsAnatomySymptoms((jsonObj.getInt("isAnatomySymptoms")));
			obj.setIsBasicSymptom((jsonObj.getInt("isBasicSymptom")));
			obj.setSymptomsCategorieID((jsonObj
					.getString("symptomsCategorieID")));
			obj.setSymptomsCategorieName((jsonObj
					.getString("symptomsCategorieName")))*/;

			Log.v("000000", "SymptomsCategorie:" + obj.toString());
			return obj;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
	
}
